<?xml version='1.0' encoding='iso-8859-1'?>
<!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
	<head>
		<title>
			int6kuart.c
			</title>
		<meta http-equiv='content-type' content='text/html;iso-8859-1'/>
		<meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
		<meta name='author' content='cmaier@cmassoc.net'/>
		<meta name='robots' content='noindex,nofollow'/>
		<link href='toolkit.css' rel='stylesheet' type='text/css'/>
		</head>
	<body>
		<div class='headerlink'>
			[<a href='int6ktone.c.html' title=' int6ktone.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='int6kwait.c.html' title=' int6kwait.c '>NEXT</a>]
			</div>
<pre>
/*====================================================================*
 *
 *   Copyright (c) 2013 Qualcomm Atheros, Inc.
 *
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or 
 *   without modification, are permitted (subject to the limitations 
 *   in the disclaimer below) provided that the following conditions 
 *   are met:
 *
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above 
 *     copyright notice, this list of conditions and the following 
 *     disclaimer in the documentation and/or other materials 
 *     provided with the distribution.
 *
 *   * Neither the name of Qualcomm Atheros nor the names of 
 *     its contributors may be used to endorse or promote products 
 *     derived from this software without specific prior written 
 *     permission.
 *
 *   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE 
 *   GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE 
 *   COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR 
 *   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 *   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 *   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 *
 *--------------------------------------------------------------------*/

/*====================================================================*
 *
 *   int6kuart.c - Atheros Serial Line Device Manager;
 *
 *
 *   Contributor(s):
 *	Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
 *	Mathieu Olivari &lt;mathieu@qca.qualcomm.com&gt;
 *
 *--------------------------------------------------------------------*/

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#if defined (WIN32)
#	include &lt;net/ethernet.h&gt;
#elif defined (__linux__)
#	include &lt;net/ethernet.h&gt;
#elif defined (__APPLE__)
#	include &lt;net/ethernet.h&gt;
#elif defined (__OpenBSD__)
#	include &lt;sys/socket.h&gt;
#	include &lt;net/if.h&gt;
#	include &lt;net/if_arp.h&gt;
#	include &lt;netinet/in.h&gt;
#	include &lt;netinet/if_ether.h&gt;
#else
#error &quot;Unknown Environment&quot;
#endif

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include &quot;../tools/getoptv.h&quot;
#include &quot;../tools/putoptv.h&quot;
#include &quot;../tools/number.h&quot;
#include &quot;../tools/memory.h&quot;
#include &quot;../tools/endian.h&quot;
#include &quot;../tools/files.h&quot;
#include &quot;../tools/flags.h&quot;
#include &quot;../tools/error.h&quot;
#include &quot;../tools/types.h&quot;
#include &quot;../serial/serial.h&quot;
#include &quot;../plc/plc.h&quot;

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include &quot;../tools/getoptv.c&quot;
#include &quot;../tools/putoptv.c&quot;
#include &quot;../tools/version.c&quot;
#include &quot;../tools/uintspec.c&quot;
#include &quot;../tools/basespec.c&quot;
#include &quot;../tools/synonym.c&quot;
#include &quot;../tools/todigit.c&quot;
#include &quot;../tools/error.c&quot;
#include &quot;../tools/checksum32.c&quot;
#include &quot;../tools/hexencode.c&quot;
#include &quot;../tools/hexdump.c&quot;
#include &quot;../tools/hexstring.c&quot;
#include &quot;../tools/hexdecode.c&quot;
#include &quot;../tools/error.c&quot;
#endif

#ifndef MAKEFILE
#include &quot;../serial/openport.c&quot;
#include &quot;../serial/closeport.c&quot;
#include &quot;../serial/serial.c&quot;
#endif

/*====================================================================*
 *   program constants;
 *--------------------------------------------------------------------*/

#define FRAME_MIN_CHAR 120
#define FRAME_MAX_CHAR 1496

/*====================================================================*
 *   program variables;
 *--------------------------------------------------------------------*/

typedef struct uart

{
	struct _file_ port;
	struct _file_ pib;
	struct _file_ nvm;
	struct _file_ eth;
	char const * string;
	char PIBVersion [3];
	char IMGVersion [128];
	byte MACAddress [ETHER_ADDR_LEN];
	char NMKDigest [16];
	byte NMKNumber;
	byte module;
	uint16_t bfsize;
	uint16_t snooze;
	uint16_t timeout;
	unsigned flags;
}

uart;

/*====================================================================*
 *
 *   void at_writenvm (struct uart * uart);
 *
 *   read firmware image from file and send to device using command
 *   &quot;ATWPF&quot;; the file descriptor is &quot;nvm&quot; member of struct uart;
 *
 *--------------------------------------------------------------------*/

static void at_writenvm (struct uart * uart)

{
	extern struct command command;
	byte memory [UART_BLOCKSIZE];
	signed mblock = sizeof (memory);
	uint16_t mlength = 0;
	uint32_t moffset = 0;
	uint32_t mchksum;
	uint16_t olength = 0;
	uint32_t ooffset = 0;
	uint32_t ochksum;
	while ((mblock = read (uart-&gt;nvm.file, memory, mblock)) &gt; 0)
	{
		clearcommand ();
		insert ('A');
		insert ('T');
		insert ('W');
		insert ('P');
		insert ('F');
		insert ('1');
		insert (',');
		mchksum = checksum32 (memory, (size_t)(mblock), 0);
		mlength = (uint16_t)(mblock);
		mlength = HTOBE16 (mlength);
		decode (&amp;mlength, sizeof (mlength));
		mlength = BE16TOH (mlength);
		insert (',');
		moffset = HTOBE32 (moffset);
		decode (&amp;moffset, sizeof (moffset));
		moffset = BE32TOH (moffset);
		insert (',');
		mchksum = HTOBE32 (mchksum);
		decode (&amp;mchksum, sizeof (mchksum));
		mchksum = BE32TOH (mchksum);
		insert (',');
		decode (memory, mlength);
		insert ('\r');
		sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
		readcommand (&amp;uart-&gt;port, uart-&gt;flags);
		mustbe ('O');
		mustbe ('K');
		mustbe ('1');
		mustbe (',');
		olength = (uint16_t)(hextoint (sizeof (olength)));
		if (olength != mlength)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected length %X&quot;, command.buffer, mlength);
		}
		mustbe (',');
		ooffset = (uint32_t)(hextoint (sizeof (ooffset)));
		if (ooffset != moffset)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected offset %X&quot;, command.buffer, moffset);
		}
		mustbe (',');
		ochksum = (uint32_t)(hextoint (sizeof (ochksum)));
		if (ochksum != mchksum)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected checksum %X (%X)&quot;, command.buffer, mchksum, ochksum);
		}
		mustbe (',');
		encode (memory, mblock);
		mustbe ('\r');
		moffset += mlength;
		if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
		{
			write (STDOUT_FILENO, &quot;.&quot;, 1);
		}
	}

#ifndef WIN32

	if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
	{
		write (STDOUT_FILENO, &quot;\n&quot;, 1);
	}

#endif

	return;
}

/*====================================================================*
 *
 *   void at_writepib (struct uart * uart);
 *
 *   read parameter block file and send to device using command
 *   &quot;ATWPF&quot;; the file descriptor is &quot;pib&quot; member of struct uart;
 *
 *--------------------------------------------------------------------*/

static void at_writepib (struct uart * uart)

{
	extern struct command command;
	byte memory [UART_BLOCKSIZE];
	signed mblock = sizeof (memory);
	uint16_t mlength = 0;
	uint16_t moffset = 0;
	uint32_t mchksum;
	uint16_t olength = 0;
	uint16_t ooffset = 0;
	uint32_t ochksum;
	while ((mblock = read (uart-&gt;pib.file, memory, mblock)) &gt; 0)
	{
		clearcommand ();
		insert ('A');
		insert ('T');
		insert ('W');
		insert ('P');
		insert ('F');
		insert ('2');
		insert (',');
		mchksum = checksum32 (memory, (size_t)(mblock), 0);
		mlength = (uint16_t)(mblock);
		mlength = HTOBE16 (mlength);
		decode (&amp;mlength, sizeof (mlength));
		mlength = BE16TOH (mlength);
		insert (',');
		moffset = HTOBE16 (moffset);
		decode (&amp;moffset, sizeof (moffset));
		moffset = BE16TOH (moffset);
		insert (',');
		mchksum = HTOBE32 (mchksum);
		decode (&amp;mchksum, sizeof (mchksum));
		mchksum = BE32TOH (mchksum);
		insert (',');
		decode (memory, mlength);
		insert ('\r');
		sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
		readcommand (&amp;uart-&gt;port, uart-&gt;flags);
		mustbe ('O');
		mustbe ('K');
		mustbe ('2');
		mustbe (',');
		olength = (uint16_t)(hextoint (sizeof (olength)));
		if (olength != mlength)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected length %X&quot;, command.buffer, mlength);
		}
		mustbe (',');
		ooffset = (uint16_t)(hextoint (sizeof (ooffset)));
		if (ooffset != moffset)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected offset %X&quot;, command.buffer, moffset);
		}
		mustbe (',');
		ochksum = (uint32_t)(hextoint (sizeof (ochksum)));
		if (ochksum != mchksum)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected checksum %X (%X)&quot;, command.buffer, mchksum, ochksum);
		}
		mustbe (',');
		encode (memory, mblock);
		mustbe ('\r');
		moffset += mlength;
		if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
		{
			write (STDOUT_FILENO, &quot;.&quot;, 1);
		}
	}

#ifndef WIN32

	if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
	{
		write (STDOUT_FILENO, &quot;\n&quot;, 1);
	}

#endif

	return;
}

/*====================================================================*
 *
 *   void at_readpib (struct uart * uart);
 *
 *   read parameter block from device and save to file using command
 *   &quot;ATRP&quot;; the file descriptor is &quot;pib&quot; member of struct uart;
 *
 *--------------------------------------------------------------------*/

static void at_readpib (struct uart * uart)

{
	extern struct command command;
	byte memory [UART_BLOCKSIZE];
	signed mblock = sizeof (memory);
	uint16_t mextent = 0;
	uint16_t mlength = 0;
	uint16_t moffset = 0;
	uint16_t olength = 0;
	uint16_t ooffset = 0;
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('R');
	insert ('P');
	insert ('2');
	insert (',');
	insert ('4');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('2');
	mustbe (',');
	mustbe ('4');
	mustbe (',');
	encode (&amp;mextent, sizeof (mextent));
	mextent = LE16TOH (mextent);
	mustbe ('\r');
	while (mextent)
	{
		clearcommand ();
		insert ('A');
		insert ('T');
		insert ('R');
		insert ('P');
		if (mblock &gt; mextent)
		{
			mblock = mextent;
		}
		mlength = (uint16_t)(mblock);
		mlength = HTOBE16 (mlength);
		decode (&amp;mlength, sizeof (mlength));
		mlength = BE16TOH (mlength);
		insert (',');
		moffset = HTOBE16 (moffset);
		decode (&amp;moffset, sizeof (moffset));
		moffset = BE16TOH (moffset);
		insert ('\r');
		sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
		readcommand (&amp;uart-&gt;port, uart-&gt;flags);
		mustbe ('O');
		mustbe ('K');
		olength = (uint16_t)(hextoint (sizeof (olength)));
		if (olength != mlength)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: have %d bytes but wanted %d&quot;, command.buffer, olength, mlength);
		}
		mustbe (',');
		ooffset = (uint16_t)(hextoint (sizeof (ooffset)));
		if (ooffset != moffset)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, EINVAL, &quot;[%s]: expected offset %X&quot;, command.buffer, moffset);
		}
		mustbe (',');
		encode (memory, mblock);
		if (write (uart-&gt;pib.file, memory, mblock) &lt; mblock)
		{
			command.buffer [command.offset] = (char)(0);
			error (1, errno, &quot;[%s]: expected length %d&quot;, command.buffer, mblock);
		}
		mustbe ('\r');
		moffset += mblock;
		mextent -= mblock;
		if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
		{
			write (STDOUT_FILENO, &quot;.&quot;, 1);
		}
	}

#ifndef WIN32

	if (_allclr (uart-&gt;flags, (UART_VERBOSE | UART_SILENCE)))
	{
		write (STDOUT_FILENO, &quot;\n&quot;, 1);
	}

#endif

	return;
}

/*====================================================================*
 *
 *   void at_wake (struct uart * uart);
 *
 *   send wake command &quot;+++&quot; to enter command mode;
 *
 *--------------------------------------------------------------------*/

static void at_wake (struct uart * uart)

{
	clearcommand ();
	insert ('+');
	insert ('+');
	insert ('+');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void at_command (struct uart * uart);
 *
 *   send custom command; use this function to send any serial line
 *   command that may not be supported by this program;
 *
 *--------------------------------------------------------------------*/

static void at_command (struct uart * uart)

{
	clearcommand ();
	while (*uart-&gt;string)
	{
		insert (*uart-&gt;string++);
	}
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	clearcommand ();
	return;
}

/*====================================================================*
 *
 *   void at_respond (struct uart * uart);
 *
 *   send command &quot;AT&quot; to test command mode; this command does nothing
 *   but echo &quot;OK&quot;;
 *
 *--------------------------------------------------------------------*/

static void at_respond (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atz (struct uart * uart);
 *
 *   send command &quot;ATZ&quot; to reset the device; no response is expected;
 *
 *--------------------------------------------------------------------*/

static void atz (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('Z');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	return;
}

/*====================================================================*
 *
 *   void atrv (struct uart * uart);
 *
 *   read and display the firmware image version using command &quot;ATRV&quot;;
 *   return the version string in IMGVersion member of struct uart;
 *
 *--------------------------------------------------------------------*/

static void atrv (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('R');
	insert ('V');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\&quot;');
	string (uart-&gt;IMGVersion);
	mustbe ('\&quot;');
	mustbe ('\r');
	printf (&quot;%s\n&quot;, uart-&gt;IMGVersion);
	return;
}

/*====================================================================*
 *
 *   void atrpm (struct uart * uart);
 *
 *   read and display the PIB version and MAC address using command
 *   &quot;ATRPM&quot;; return version string in PIBVersion member and address
 *   string in MACAddress member of struct
 *
 *
 *--------------------------------------------------------------------*/

static void atrpm (struct uart * uart)

{
	char mac [ETHER_ADDR_LEN * 3];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('R');
	insert ('P');
	insert ('M');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\&quot;');
	string (uart-&gt;PIBVersion);
	mustbe ('\&quot;');
	mustbe (',');
	encode (uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress));
	mustbe ('\r');
	printf (&quot;%s %s\n&quot;, uart-&gt;PIBVersion, hexstring (mac, sizeof (mac), uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress)));
	return;
}

/*====================================================================*
 *
 *   void atsk1 (struct uart * uart);
 *
 *   send Set Key command &quot;ATSK&quot;; ask device for NMK; encode returned
 *   key into uart-NMK;
 *
 *--------------------------------------------------------------------*/

static void atsk1 (struct uart * uart)

{
	char key [48];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('S');
	insert ('K');
	insert ('?');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	encode (uart-&gt;NMKDigest, sizeof (uart-&gt;NMKDigest));
	mustbe ('\r');
	printf (&quot;%s\n&quot;, hexstring (key, sizeof (key), uart-&gt;NMKDigest, sizeof (uart-&gt;NMKDigest)));
	return;
}

/*====================================================================*
 *
 *   void atsk2 (struct uart * uart);
 *
 *   send Set Key command &quot;ATSK&quot;; send device the NMK; encode returned
 *
 *--------------------------------------------------------------------*/

static void atsk2 (struct uart * uart)

{
	char key [48];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('S');
	insert ('K');
	decode (uart-&gt;NMKDigest, sizeof (uart-&gt;NMKDigest));
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	encode (uart-&gt;NMKDigest, sizeof (uart-&gt;NMKDigest));
	mustbe ('\r');
	printf (&quot;%s\n&quot;, hexstring (key, sizeof (key), uart-&gt;NMKDigest, sizeof (uart-&gt;NMKDigest)));
	return;
}

/*====================================================================*
 *
 *   void atdst1 (struct uart * uart);
 *
 *   read transparent mode destination MAC address command &quot;ATDST?&quot;;
 *
 *--------------------------------------------------------------------*/

static void atdst1 (struct uart * uart)

{
	char mac [ETHER_ADDR_LEN * 3];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('D');
	insert ('S');
	insert ('T');
	insert ('?');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	encode (uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress));
	mustbe ('\r');
	printf (&quot;%s\n&quot;, hexstring (mac, sizeof (mac), uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress)));
	return;
}

/*====================================================================*
 *
 *   void atdst2 (struct uart * uart);
 *
 *   read transparent mode destination MAC address command &quot;ATDST?&quot;;
 *
 *--------------------------------------------------------------------*/

static void atdst2 (struct uart * uart)

{
	char mac [ETHER_ADDR_LEN * 3];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('D');
	insert ('S');
	insert ('T');
	decode (uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress));
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	encode (uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress));
	mustbe ('\r');
	printf (&quot;%s\n&quot;, hexstring (mac, sizeof (mac), uart-&gt;MACAddress, sizeof (uart-&gt;MACAddress)));
	return;
}

/*====================================================================*
 *
 *   void atni (struct uart * uart);
 *
 *   reset device to factory default pib command &quot;ATNI&quot;;
 *
 *--------------------------------------------------------------------*/

static void atni (struct uart * uart)

{
	unsigned count;
	unsigned index;
	uint16_t rxrate;
	uint16_t txrate;
	byte address [ETHER_ADDR_LEN];
	char mac [ETHER_ADDR_LEN * 3];
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('N');
	insert ('I');
	insert ('?');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	count = (unsigned)(hextoint (sizeof (unsigned)));
	while (count--)
	{
		mustbe (',');
		index = (unsigned)(hextoint (sizeof (index)));
		mustbe (',');
		encode (address, sizeof (address));
		mustbe (',');
		txrate = (uint16_t)(hextoint (sizeof (rxrate)));
		mustbe (',');
		rxrate = (uint16_t)(hextoint (sizeof (txrate)));
		printf (&quot;%d %s %3d RX %3d TX\n&quot;, index, hexstring (mac, sizeof (mac), address, sizeof (address)), rxrate, txrate);
	}
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atfd (struct uart * uart);
 *
 *   reset device to factory default pib command &quot;ATFD&quot;;
 *
 *--------------------------------------------------------------------*/

static void atfd (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('F');
	insert ('D');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atps (struct uart * uart);
 *
 *   etner power save mode command &quot;ATPS&quot;;
 *
 *--------------------------------------------------------------------*/

static void atps (struct uart * uart)

{
	extern struct command command;
	uint16_t result;
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('P');
	insert ('S');
	uart-&gt;snooze = HTOBE16 (uart-&gt;snooze);
	decode (&amp;uart-&gt;snooze, sizeof (uart-&gt;snooze));
	uart-&gt;snooze = BE16TOH (uart-&gt;snooze);
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	result = (uint16_t)(hextoint (sizeof (result)));
	if (result != uart-&gt;snooze)
	{
		error (1, EINVAL, &quot;[%s]: expected timeout %04X&quot;, command.buffer, uart-&gt;snooze);
	}
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void ato (struct uart * uart);
 *
 *   exit command mode and enter tyransparent mode command &quot;ATO&quot;;
 *
 *--------------------------------------------------------------------*/

static void ato (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('O');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void athsc (struct uart * uart);
 *
 *   exit command mode; enter high speed command mode &quot;ATHSC&quot;;
 *
 *--------------------------------------------------------------------*/

static void athsc (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('H');
	insert ('S');
	insert ('C');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atwnv (struct uart * uart);
 *
 *   write PIB and/or IMG to NVM &quot;ATWNVx&quot;;
 *
 *--------------------------------------------------------------------*/

static void atwnv (struct uart * uart)

{
	extern struct command command;
	byte result;
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('W');
	insert ('N');
	insert ('V');
	decode (&amp;uart-&gt;module, sizeof (uart-&gt;module));
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	result = (byte)(hextoint (sizeof (result)));
	if (result != uart-&gt;module)
	{
		error (1, EINVAL, &quot;[%s]: expected module %d&quot;, command.buffer, uart-&gt;module);
	}
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atbsz1 (struct uart * uart);
 *
 *   get transparent mode buffer size &quot;ATBSZ?&quot;;
 *
 *--------------------------------------------------------------------*/

static void atbsz1 (struct uart * uart)

{
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('B');
	insert ('S');
	insert ('Z');
	insert ('?');
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	uart-&gt;bfsize = (uint16_t)(hextoint (sizeof (uart-&gt;bfsize)));
	mustbe ('\r');
	printf (&quot;%d\n&quot;, uart-&gt;bfsize);
	return;
}

/*====================================================================*
 *
 *   void atbsz2 (struct uart * uart);
 *
 *   set transparent mode buffer size &quot;ATBSZn&quot;;
 *
 *--------------------------------------------------------------------*/

static void atbsz2 (struct uart * uart)

{
	extern struct command command;
	uint16_t result;
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('B');
	insert ('S');
	insert ('Z');
	uart-&gt;bfsize = HTOBE16 (uart-&gt;bfsize);
	decode (&amp;uart-&gt;bfsize, sizeof (uart-&gt;bfsize));
	uart-&gt;bfsize = BE16TOH (uart-&gt;bfsize);
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	result = (uint16_t)(hextoint (sizeof (result)));
	if (result != uart-&gt;bfsize)
	{
		error (1, EINVAL, &quot;[%s]: expected buffer size %04X&quot;, command.buffer, uart-&gt;bfsize);
	}
	mustbe ('\r');
	printf (&quot;%d\n&quot;, uart-&gt;bfsize);
	return;
}

/*====================================================================*
 *
 *   void atto (struct uart * uart);
 *
 *   set transparent mode buffer timeout &quot;ATTO&quot;;
 *
 *--------------------------------------------------------------------*/

static void atto (struct uart * uart)

{
	extern struct command command;
	uint16_t result;
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('T');
	insert ('O');
	uart-&gt;timeout = HTOBE16 (uart-&gt;timeout);
	decode (&amp;uart-&gt;timeout, sizeof (uart-&gt;timeout));
	uart-&gt;timeout = BE16TOH (uart-&gt;timeout);
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	mustbe ('O');
	mustbe ('K');
	result = (uint16_t)(hextoint (sizeof (result)));
	if (result != uart-&gt;timeout)
	{
		error (1, EINVAL, &quot;[%s]: expected timeout %04X&quot;, command.buffer, uart-&gt;timeout);
	}
	mustbe ('\r');
	return;
}

/*====================================================================*
 *
 *   void atm (struct uart * uart);
 *
 *
 *--------------------------------------------------------------------*/

static void atm (struct uart * uart)

{
	extern struct command command;
	uint8_t buffer [ETHER_MAX_LEN + ETHER_MAX_LEN + 512];
	unsigned length = (unsigned)(readframe (uart-&gt;eth.file, buffer, sizeof (buffer)));
	if (length &lt; FRAME_MIN_CHAR)
	{
		error (1, ENOTSUP, &quot;Frame specification of %d bytes less than %d minimum&quot;, (length &gt;&gt; 1), (FRAME_MIN_CHAR &gt;&gt; 1));
	}
	if (length &gt; FRAME_MAX_CHAR)
	{
		error (1, ENOTSUP, &quot;Frame specification of %d bytes more than %d maximum&quot;, (length &gt;&gt; 1), (FRAME_MAX_CHAR &gt;&gt; 1));
	}
	clearcommand ();
	insert ('A');
	insert ('T');
	insert ('M');
	memcpy (command.buffer + command.length, buffer, length);
	command.length += (signed)(length);
	insert ('\r');
	sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
	readcommand (&amp;uart-&gt;port, uart-&gt;flags);
	write (STDOUT_FILENO, command.buffer, command.length);
	write (STDOUT_FILENO, &quot;\n&quot;, sizeof (char));
	return;
}

/*====================================================================*
 *
 *   void manager (struct uart * uart);
 *
 *   examine flagword in struct uart and perform requested operations
 *   in the order that bits are tested; the order that bits are tested
 *   may be changed as needed;
 *
 *--------------------------------------------------------------------*/

static void manager (struct uart * uart)

{
	if (_anyset (uart-&gt;flags, UART_WAKE))
	{
		at_wake (uart);
	}
	if (_anyset (uart-&gt;flags, UART_COMMAND))
	{
		at_command (uart);
	}
	if (_anyset (uart-&gt;flags, UART_RESPOND))
	{
		at_respond (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATRV))
	{
		atrv (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATRPM))
	{
		atrpm (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATDST1))
	{
		atdst1 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATDST2))
	{
		atdst2 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATZ))
	{
		atz (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATFD))
	{
		atfd (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATPS))
	{
		atps (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATO))
	{
		ato (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATNI))
	{
		atni (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATHSC))
	{
		athsc (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATSK1))
	{
		atsk1 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATSK2))
	{
		atsk2 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATRP))
	{
		at_readpib (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATWPF1))
	{
		at_writenvm (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATWPF2))
	{
		at_writepib (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATWNV))
	{
		atwnv (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATBSZ1))
	{
		atbsz1 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATBSZ2))
	{
		atbsz2 (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATM))
	{
		atm (uart);
	}
	if (_anyset (uart-&gt;flags, UART_ATTO))
	{
		atto (uart);
	}
	return;
}

/*====================================================================*
 *
 *   int main (int argc, char const * argv []);
 *
 *
 *
 *
 *--------------------------------------------------------------------*/

int main (int argc, char const * argv [])

{
	static char const * optv [] =
	{
		&quot;bc:C:dD:F:HiImM:n:N:Op:P:qrRS:s:tTvwW:zZ:&quot;,
		&quot;&quot;,
		&quot;Atheros Serial Line Device Manager&quot;,
		&quot;b\tset default host baud rate&quot;,
		&quot;c s\tsend custom serial line command (s)&quot;,
		&quot;C x\tcommit module (x) to NVM [ATWNVx]&quot;,
		&quot;d\tget destination mac address [ATDST?]&quot;,
		&quot;D x\tset destination mac address [ATDSTx]&quot;,
		&quot;F f\tframe file is (s)&quot;,
		&quot;H\tplace device in High Speed Command Mode [ATHSC]&quot;,
		&quot;i\tget network information [ATNI]&quot;,
		&quot;I\tget PIB version and MAC address [ATRPM]&quot;,
		&quot;m\tget network membership key [ATSK?]&quot;,
		&quot;M x\tset network membership key [ATSKx]&quot;,
		&quot;N f\twrite NVM file (f) to SDRAM [ATWFP1]&quot;,
		&quot;O\tplace device in Transparent Mode [ATO]&quot;,
		&quot;p f\tread PIB from SDRAM to file (f) [ATRP]&quot;,
		&quot;P f\twrite PIB file (f) to SDRAM [ATWFP2]&quot;,
		&quot;q\tplace program in quiet mode&quot;,
		&quot;r\tget parameter/firmware revision [ATRV]&quot;,
		&quot;R\treset device [ATZ]&quot;,
		&quot;s f\tserial port is (f) [&quot; DEVICE &quot;]&quot;,
		&quot;S n\tenter power save mode for (n) seconds [ATPS]&quot;,
		&quot;t\ttest device [AT]&quot;,
		&quot;T\treset to factory defaults [ATFD]&quot;,
		&quot;v\tplace program verbose mode&quot;,
		&quot;w\tplace device in Command Mode [+++]&quot;,
		&quot;W x\tset Transparent Mode aggregation timeout [ATTO]&quot;,
		&quot;z\tget Transparent Mode buffer size [ATBSZ?]&quot;,
		&quot;Z n\tset Transparent Mode buffer size [ATBSZn]&quot;,
		(char const *) (0)
	};
	struct uart uart =
	{
		{
			0,
			DEVICE
		},
		{
			-1,
			&quot;nvmfile&quot;
		},
		{
			-1,
			&quot;pibfile&quot;
		},
		{
			-1,
			&quot;ethfile&quot;
		},
		(char *)(0),
		{
			0
		},
		{
			0
		},
		{
			0
		},
		{
			0
		},
		(uint8_t)(0),
		(uint8_t)(0),
		(uint16_t)(0),
		(uint16_t)(0),
		(uint16_t)(0),
		(unsigned)(0)
	};
	signed c;
	if (getenv (UART_PORT))
	{
		uart.port.name = strdup (getenv (UART_PORT));
	}
	while ((c = getoptv (argc, argv, optv)) != -1)
	{
		switch (c)
		{
		case 'b':
			_setbits (uart.flags, UART_DEFAULT);
			break;
		case 'c':
			_setbits (uart.flags, UART_COMMAND);
			uart.string = optarg;
			break;
		case 'C':
			_setbits (uart.flags, UART_ATWNV);
			uart.module = (byte)(basespec (optarg, 16, sizeof (uart.module)));
			break;
		case 'd':
			_setbits (uart.flags, UART_ATDST1);
			break;
		case 'D':
			_setbits (uart.flags, UART_ATDST2);
			if (!hexencode (uart.MACAddress, sizeof (uart.MACAddress), optarg))
			{
				error (1, errno, PLC_BAD_MAC, optarg);
			}
			break;
		case 'F':
			if ((uart.eth.file = open (uart.eth.name = optarg, O_BINARY | O_RDONLY)) == -1)
			{
				error (1, errno, &quot;%s&quot;, uart.eth.name);
			}
			_setbits (uart.flags, UART_ATM);
			break;
		case 'H':
			_setbits (uart.flags, UART_ATHSC);
			break;
		case 'i':
			_setbits (uart.flags, UART_ATNI);
			break;
		case 'I':
			_setbits (uart.flags, UART_ATRPM);
			break;
		case 'm':
			_setbits (uart.flags, UART_ATSK1);
			break;
		case 'M':
			_setbits (uart.flags, UART_ATSK2);
			if (!hexencode (uart.NMKDigest, sizeof (uart.NMKDigest), optarg))
			{
				error (1, errno, PLC_BAD_NMK, optarg);
			}
			break;
		case 'N':
			if ((uart.nvm.file = open (uart.nvm.name = optarg, O_BINARY | O_RDONLY)) == -1)
			{
				error (1, errno, &quot;%s&quot;, uart.nvm.name);
			}
			_setbits (uart.flags, UART_ATWPF1);
			break;
		case 'O':
			_setbits (uart.flags, UART_ATO);
			break;
		case 'P':
			if ((uart.pib.file = open (uart.pib.name = optarg, O_BINARY | O_RDONLY)) == -1)
			{
				error (1, errno, &quot;%s&quot;, uart.pib.name);
			}
			_setbits (uart.flags, UART_ATWPF2);
			break;
		case 'p':
			if ((uart.pib.file = open (uart.pib.name = optarg, O_BINARY|O_CREAT|O_RDWR|O_TRUNC, FILE_FILEMODE)) == -1)
			{
				error (1, errno, &quot;%s&quot;, uart.pib.name);
			}

#ifndef WIN32

			chown (optarg, getuid (), getgid ());

#endif

			_setbits (uart.flags, UART_ATRP);
			break;
		case 'q':
			_setbits (uart.flags, UART_SILENCE);
			break;
		case 'r':
			_setbits (uart.flags, UART_ATRV);
			break;
		case 'R':
			_setbits (uart.flags, UART_ATZ);
			break;
		case 'S':
			_setbits (uart.flags, UART_ATPS);
			uart.snooze = (uint16_t)(uintspec (optarg, 1, 900));
			break;
		case 's':
			uart.port.name = optarg;
			break;
		case 'T':
			_setbits (uart.flags, UART_ATFD);
			break;
		case 't':
			_setbits (uart.flags, UART_RESPOND);
			break;
		case 'v':
			_setbits (uart.flags, UART_VERBOSE);
			break;
		case 'w':
			_setbits (uart.flags, UART_WAKE);
			break;
		case 'W':
			_setbits (uart.flags, UART_ATTO);
			uart.timeout = (unsigned)(uintspec (optarg, 1, 2000));
			break;
		case 'z':
			_setbits (uart.flags, UART_ATBSZ1);
			break;
		case 'Z':
			_setbits (uart.flags, UART_ATBSZ2);
			uart.bfsize = (uint16_t)(uintspec (optarg, 1, 1500));
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	if (argc)
	{
		error (1, ENOTSUP, ERROR_TOOMANY);
	}
	openport (&amp;uart.port, uart.flags);
	manager (&amp;uart);
	closeport (&amp;uart.port);
	exit (0);
}


</pre>
		<div class='footerlink'>
			[<a href='int6ktone.c.html' title=' int6ktone.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='int6kwait.c.html' title=' int6kwait.c '>NEXT</a>]
			</div>
		</body>
	</html>
